<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
			
			window.onload = function(){
				//获取img标签
				var img1 = document.getElementById("img1");
				
				//创建一个数组
				var imgArr =["img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg","img/5.jpg"];
				
				//创建一个变量用来保存当前的值
				var index = 0;
				
				//定义一个变量，用来保存定时器的标识
				var timer;
				
				//获取开始按钮
				var btn01 = document.getElementById("btn01");
				//为开始按钮绑定单机响应函数
				btn01.onclick = function(){
					
					//在开启定时器之前，需要先将当前元素上的其他定时器关闭
					clearInterval(timer);
					//开启一个计时器
					timer = setInterval(function(){
						//使用自增
						index++;
						
						//判断索引是否超过最大索引
						/*if(index >= imgArr.length){
							index = 0;
						}*/
						
						index %= imgArr.length;
						
						
						//修改img1的属性
						img1.src = imgArr[index];
						
					},1000);
				};
				 
				//为btn02绑定一个暂停时间
				var btn02 = document.getElementById("btn02");
				btn02.onclick = function(){
					clearInterval(timer);
				};
		
			};
			
		</script>
	</head>
	<body>
		<img id="img1" src="img/1.jpg"/>
		<br />
		<button id="btn01">开始</button>
		<button id="btn02">暂停</button>
	</body>
</html>
